package com.commodity.Dao.userDaoImp;

import com.commodity.Dao.UserDao;
import com.commodity.common.Global;
import com.commodity.entity.User;
import com.commodity.utils.Md5Utils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class UserDaoImp implements UserDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(Global.getDataSource());
    RowMapper<User> MAPPER = new BeanPropertyRowMapper<>(User.class);
    @Override
    public User findByUserName(String username) {
        String sql = "select * from t_user where username = ?";
        List<User> userList = jdbcTemplate.query(sql,MAPPER,username);
        return userList.isEmpty()?null:userList.get(0);
    }

    @Override
    public Integer editPwd(String username, String password) {
        String password1 = Md5Utils.md5(password+"{"+username+"}");
        String sql = "update t_user set password = ? where username = ?";
        int rows = jdbcTemplate.update(sql,password1,username);
        return rows;
    }
}
